package com.punai.apply.controller;

import cn.hutool.core.io.FileUtil;
import com.punai.apply.domain.ApplyCostMould;
import com.punai.apply.service.IApplyCostMouldService;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.properties.SqliteUpPathConfig;
import com.punai.common.utils.StringUtils;
import com.punai.common.utils.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 成本编制模板管理Controller
 * 
 * @author zhx
 * @date 2023-03-21
 */
@RestController
@RequestMapping("/apply/applyCostMould")
public class ApplyCostMouldController extends BaseController
{
    @Autowired
    private IApplyCostMouldService applyCostMouldService;

    /**
     * 查询成本编制模板管理列表
     */
    @PreAuthorize("@ss.hasPermi('apply:applyCostMould:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApplyCostMould applyCostMould)
    {
        startPage();
        applyCostMould.setCompanyId(getCompanyId());
        List<ApplyCostMould> list = applyCostMouldService.selectApplyCostMouldList(applyCostMould);
        return getDataTable(list);
    }

    /**
     * 获取成本编制模板管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('apply:applyCostMould:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(applyCostMouldService.selectApplyCostMouldById(id));
    }

    /**
     * 新增成本编制模板管理
     */
//    @PreAuthorize("@ss.hasPermi('apply:applyCostMould:add')")
    @Log(title = "成本编制模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApplyCostMould applyCostMould)
    {
        applyCostMould.setCompanyId(getCompanyId());
        return toAjax(applyCostMouldService.insertApplyCostMould(applyCostMould));
    }

    /**
     * 修改成本编制模板管理
     */
//    @PreAuthorize("@ss.hasPermi('apply:applyCostMould:edit')")
    @Log(title = "成本编制模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApplyCostMould applyCostMould)
    {
        return toAjax(applyCostMouldService.updateApplyCostMould(applyCostMould));
    }

    @Log(title = "下载", businessType = BusinessType.INSERT)
    @PostMapping(value = "/export")
    public AjaxResult export(String id, HttpServletResponse response)
    {
        if (StringUtils.isEmpty(id)) {
            AjaxResult.error("id获取失败");
        }
        String filePath = applyCostMouldService.export(id);

        String zipFilePath = SqliteUpPathConfig.getTemplatezippath() + System.currentTimeMillis();
        // 压缩
        File file = new File(filePath);
        File[] files = file.listFiles();
        try {
            ZipUtils.toZip(Arrays.asList(files), new FileOutputStream(zipFilePath));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        downloadFile(FileUtil.getInputStream(new File(zipFilePath)),response);




        return AjaxResult.success(applyCostMouldService.export(id));
    }
}
